﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using WISD.DBUtility.DBOperate;

namespace WISD.CarFriend.BLL.OA
{
    public partial class ShopModule
    {
        public ShopModule()
        { }


        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("Id", "T_CF_OA_Shop");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_CF_OA_Shop");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(WISD.CarFriend.Model.CFModel.T_CF_OA_Shop model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_CF_OA_Shop(");
            strSql.Append("ShopName,Address,Leader,Phone,Mobile,IsDel,Memo,Memo1,Memo2,Memo3,Memo4,Memo5,Memo6,Memo7,Memo8)");
            strSql.Append(" values (");
            strSql.Append("@ShopName,@Address,@Leader,@Phone,@Mobile,@IsDel,@Memo,@Memo1,@Memo2,@Memo3,@Memo4,@Memo5,@Memo6,@Memo7,@Memo8)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@ShopName", SqlDbType.NVarChar,50),
					new SqlParameter("@Address", SqlDbType.NVarChar,500),
					new SqlParameter("@Leader", SqlDbType.NVarChar,50),
					new SqlParameter("@Phone", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@IsDel", SqlDbType.Int,4),
					new SqlParameter("@Memo", SqlDbType.NVarChar,500),
					new SqlParameter("@Memo1", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo2", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo3", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo4", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo5", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo6", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo7", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo8", SqlDbType.NVarChar,100)};
            parameters[0].Value = model.ShopName;
            parameters[1].Value = model.Address;
            parameters[2].Value = model.Leader;
            parameters[3].Value = model.Phone;
            parameters[4].Value = model.Mobile;
            parameters[5].Value = model.IsDel;
            parameters[6].Value = model.Memo;
            parameters[7].Value = model.Memo1;
            parameters[8].Value = model.Memo2;
            parameters[9].Value = model.Memo3;
            parameters[10].Value = model.Memo4;
            parameters[11].Value = model.Memo5;
            parameters[12].Value = model.Memo6;
            parameters[13].Value = model.Memo7;
            parameters[14].Value = model.Memo8;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(WISD.CarFriend.Model.CFModel.T_CF_OA_Shop model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_CF_OA_Shop set ");
            strSql.Append("ShopName=@ShopName,");
            strSql.Append("Address=@Address,");
            strSql.Append("Leader=@Leader,");
            strSql.Append("Phone=@Phone,");
            strSql.Append("Mobile=@Mobile,");
            strSql.Append("IsDel=@IsDel,");
            strSql.Append("Memo=@Memo,");
            strSql.Append("Memo1=@Memo1,");
            strSql.Append("Memo2=@Memo2,");
            strSql.Append("Memo3=@Memo3,");
            strSql.Append("Memo4=@Memo4,");
            strSql.Append("Memo5=@Memo5,");
            strSql.Append("Memo6=@Memo6,");
            strSql.Append("Memo7=@Memo7,");
            strSql.Append("Memo8=@Memo8");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@ShopName", SqlDbType.NVarChar,50),
					new SqlParameter("@Address", SqlDbType.NVarChar,500),
					new SqlParameter("@Leader", SqlDbType.NVarChar,50),
					new SqlParameter("@Phone", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@IsDel", SqlDbType.Int,4),
					new SqlParameter("@Memo", SqlDbType.NVarChar,500),
					new SqlParameter("@Memo1", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo2", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo3", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo4", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo5", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo6", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo7", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo8", SqlDbType.NVarChar,100),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.ShopName;
            parameters[1].Value = model.Address;
            parameters[2].Value = model.Leader;
            parameters[3].Value = model.Phone;
            parameters[4].Value = model.Mobile;
            parameters[5].Value = model.IsDel;
            parameters[6].Value = model.Memo;
            parameters[7].Value = model.Memo1;
            parameters[8].Value = model.Memo2;
            parameters[9].Value = model.Memo3;
            parameters[10].Value = model.Memo4;
            parameters[11].Value = model.Memo5;
            parameters[12].Value = model.Memo6;
            parameters[13].Value = model.Memo7;
            parameters[14].Value = model.Memo8;
            parameters[15].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            //StringBuilder strSql = new StringBuilder();
            //strSql.Append("delete from T_CF_OA_Shop ");
            //strSql.Append(" where Id=@Id");
            //SqlParameter[] parameters = {
            //        new SqlParameter("@Id", SqlDbType.Int,4)
            //};
            //parameters[0].Value = Id;

            //int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            //if (rows > 0)
            //{
            //    return true;
            //}
            //else
            //{
            //    return false;
            //}

            //逻辑删除
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" UPDATE T_CF_OA_Shop ");
            strSql.Append(" SET IsDel = 1 ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@Id", SqlDbType.Int,4)
            };
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_CF_OA_Shop ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public WISD.CarFriend.Model.CFModel.T_CF_OA_Shop GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,ShopName,Address,Leader,Phone,Mobile,IsDel,Memo,Memo1,Memo2,Memo3,Memo4,Memo5,Memo6,Memo7,Memo8 from T_CF_OA_Shop ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            WISD.CarFriend.Model.CFModel.T_CF_OA_Shop model = new WISD.CarFriend.Model.CFModel.T_CF_OA_Shop();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ShopName"] != null && ds.Tables[0].Rows[0]["ShopName"].ToString() != "")
                {
                    model.ShopName = ds.Tables[0].Rows[0]["ShopName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Address"] != null && ds.Tables[0].Rows[0]["Address"].ToString() != "")
                {
                    model.Address = ds.Tables[0].Rows[0]["Address"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Leader"] != null && ds.Tables[0].Rows[0]["Leader"].ToString() != "")
                {
                    model.Leader = ds.Tables[0].Rows[0]["Leader"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Phone"] != null && ds.Tables[0].Rows[0]["Phone"].ToString() != "")
                {
                    model.Phone = ds.Tables[0].Rows[0]["Phone"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Mobile"] != null && ds.Tables[0].Rows[0]["Mobile"].ToString() != "")
                {
                    model.Mobile = ds.Tables[0].Rows[0]["Mobile"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IsDel"] != null && ds.Tables[0].Rows[0]["IsDel"].ToString() != "")
                {
                    model.IsDel = int.Parse(ds.Tables[0].Rows[0]["IsDel"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Memo"] != null && ds.Tables[0].Rows[0]["Memo"].ToString() != "")
                {
                    model.Memo = ds.Tables[0].Rows[0]["Memo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo1"] != null && ds.Tables[0].Rows[0]["Memo1"].ToString() != "")
                {
                    model.Memo1 = ds.Tables[0].Rows[0]["Memo1"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo2"] != null && ds.Tables[0].Rows[0]["Memo2"].ToString() != "")
                {
                    model.Memo2 = ds.Tables[0].Rows[0]["Memo2"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo3"] != null && ds.Tables[0].Rows[0]["Memo3"].ToString() != "")
                {
                    model.Memo3 = ds.Tables[0].Rows[0]["Memo3"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo4"] != null && ds.Tables[0].Rows[0]["Memo4"].ToString() != "")
                {
                    model.Memo4 = ds.Tables[0].Rows[0]["Memo4"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo5"] != null && ds.Tables[0].Rows[0]["Memo5"].ToString() != "")
                {
                    model.Memo5 = ds.Tables[0].Rows[0]["Memo5"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo6"] != null && ds.Tables[0].Rows[0]["Memo6"].ToString() != "")
                {
                    model.Memo6 = ds.Tables[0].Rows[0]["Memo6"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo7"] != null && ds.Tables[0].Rows[0]["Memo7"].ToString() != "")
                {
                    model.Memo7 = ds.Tables[0].Rows[0]["Memo7"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo8"] != null && ds.Tables[0].Rows[0]["Memo8"].ToString() != "")
                {
                    model.Memo8 = ds.Tables[0].Rows[0]["Memo8"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,ShopName,Address,Leader,Phone,Mobile,IsDel,Memo,Memo1,Memo2,Memo3,Memo4,Memo5,Memo6,Memo7,Memo8 ");
            strSql.Append(" FROM T_CF_OA_Shop ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,ShopName,Address,Leader,Phone,Mobile,IsDel,Memo,Memo1,Memo2,Memo3,Memo4,Memo5,Memo6,Memo7,Memo8 ");
            strSql.Append(" FROM T_CF_OA_Shop ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_CF_OA_Shop ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Id desc");
            }
            strSql.Append(")AS Row, T.*  from T_CF_OA_Shop T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

    }
}


